package com.copa.l74;

/**
 * @author copa
 * @createDate 2022-06-24 09:07
 * @function 二分查找法
 */
public class Test2 {

    public boolean searchMatrix(int[][] matrix, int target) {
        int left = 0;
        int right = matrix.length * matrix[0].length - 1;
        int mid;
        int col = matrix[0].length;
        // 这个是二维数组转化为一位数组，这样就是全局二维数组都用上二分查找法（但保证每个列长度都是一样的）
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (matrix[mid / col][mid % col] == target) {
                return true;
            }
            if (matrix[mid / col][mid % col] > target) {
                right = mid - 1;
                continue;
            }
            left = mid + 1;
        }
        return false;
    }

    public static void main(String[] args) {
        int[][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
        //int[][] matrix = {{1}};
        //int[][] matrix = {{1,3}};
        //int[][] matrix = {{1},{3}};
        System.out.println(new Test2().searchMatrix(matrix, 13));
    }
}
